﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.Model.SearchModels;
using DormMS.Model.ViewModels;
using DormMS.Model;
using Infrastructure;

namespace WeChat.Controllers
{
    public class ProductController : BaseController
    {
        private IProductService productservice;
        private IProductCategoryService categoryservice;
        private ISystemService systemservice;
        private IExchangeService exchangeservice;
        private ISpecService specService;

        public ProductController(IMemberService s, IProductService _productservice, IProductCategoryService _categoryservice, ISystemService _systemservice, IExchangeService _exchangeservice, ISpecService _specService) : base(s)
        {
            productservice = _productservice;
            categoryservice = _categoryservice;
            systemservice = _systemservice;
            exchangeservice = _exchangeservice;
            specService = _specService;
        }

        /// <summary>
        /// 商品详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ProductDetail()
        {
            return View();
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        /// <param name="Sku"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductDetail(string Sku, int ActivityId)
        {
            return Json(productservice.ProductDetail(Sku, ActivityId, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 商品评论列表，滑动加载
        /// </summary>
        /// <param name="Skip"></param>
        /// <param name="Take"></param>
        /// <param name="Sku"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CommentList(int Skip, int Take, string Sku)
        {
            int total = 0;
            var list = productservice.CommentList(Skip, Take, Sku, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 收藏商品
        /// </summary>
        /// <param name="Sku"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductFavorite(string Sku)
        {
            return Json(productservice.ProductFavorite(Sku, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="Sku"></param>
        /// <param name="SpecId"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddShopCart(string Sku, int SpecId, int Num)
        {
            return Json(productservice.AddShopCart(Sku, SpecId, Num, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 购物车
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ShopCart()
        {
            return View();
        }

        /// <summary>
        /// 获取购物车列表，滑动加载
        /// </summary>
        /// <param name="Skip"></param>
        /// <param name="Take"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ShopCartList()
        {
            return Json(productservice.ShopCartList(CurrentLoginUser.Id));
        }

        /// <summary>
        /// 购物车数量变更
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangeShopCartNum(int Id, int Num)
        {
            return Json(productservice.ChangeShopCartNum(Id, Num, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 删除购物车商品
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelCart(int Id)
        {
            return Json(productservice.DelCart(Id, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 我的收藏
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult MyFavorite()
        {
            return View();
        }

        /// <summary>
        /// 我的收藏列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetMyFavorite(int Skip, int Take)
        {
            int total = 0;
            var list = productservice.GetMyFavorite(Skip, Take, CurrentLoginUser.Id, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CancelFavorite(int Id)
        {
            return Json(productservice.CancelFavorite(Id, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 商品列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ProductList()
        {
            return View();
        }

        /// <summary>
        /// 商品列表数据，滑动加载
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductList(WapProductSearchModel search)
        {
            int total = 0;
            var list = productservice.ProductList(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetCategoryList()
        {
            return Json(categoryservice.GetCategoryListForWap());
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SearchIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取热门分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetHotCategory()
        {
            return Json(categoryservice.GetHotCategory());
        }


        [HttpGet]
        public ActionResult Search()
        {
            return View();
        }


        [HttpPost]
        public ActionResult SearchHistoryList(int Skip, int Take)
        {
            int total = 0;
            var list = _memberService.SearchHistoryList(Skip, Take, CurrentLoginUser.Id, out total);
            return Json(new { Total = total, Results = list });
        }


        [HttpPost]
        public ActionResult AddSearchHistory(string keywords)
        {
            return Json(_memberService.AddSearchHistory(CurrentLoginUser.Id, keywords));
        }


        [HttpPost]
        public ActionResult DelSearchHistory()
        {
            return Json(_memberService.DelSearchHistory(CurrentLoginUser.Id));
        }


        [HttpPost]
        public ActionResult GetSearchKeywords()
        {
            return Json(systemservice.GetSearchKeywords());
        }


        [HttpGet]
        public ActionResult ProductExchangeDetail()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ProductExchangeDetail(int Id)
        {
            return Json(exchangeservice.ProductExchangeDetail(Id, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 商品评论
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddComment()
        {
            return View();
        }

        /// <summary>
        /// 商品评论数据
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetOrderComment(int orderId)
        {
            return Json(productservice.GetOrderComment(orderId));
        }

        /// <summary>
        /// 保存评论
        /// </summary>
        /// <param name="comments"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult OrderCommentSubmit(List<AddCommentResponse> comments, int orderId)
        {

            foreach (var item in comments)
            {
                if (!item.CommentLevel.HasValue)
                {
                    Result result = new Result();
                    result.msg = "请选择满意度";
                    return Json(result);
                }
            }

            return Json(productservice.OrderCommentSubmit(comments, orderId, CurrentLoginUser.Id, Request.UserHostAddress));
        }


    

    }
}